export function caculateTimeago(dateTimeStamp) {
	const minute = 1000 * 60; // 把分，时，天，周，半个月，一个月用毫秒表示

	const hour = minute * 60;
	const day = hour * 24;
	const week = day * 7;
	const now = new Date().getTime(); // 获取当前时间毫秒

	const diffValue = now - dateTimeStamp; // 时间差

	let result = '';

	if (diffValue < 0) {
		return;
	}

	const minC = diffValue / minute; // 计算时间差的分，时，天，周，月

	const hourC = diffValue / hour;
	const dayC = diffValue / day;
	const weekC = diffValue / week;

	if (weekC >= 1 && weekC <= 4) {
		result = ` ${parseInt(weekC, 10)}周前`;
	} else if (dayC >= 1 && dayC <= 6) {
		result = ` ${parseInt(dayC, 10)}天前`;
	} else if (hourC >= 1 && hourC <= 23) {
		result = ` ${parseInt(hourC, 10)}小时前`;
	} else if (minC >= 1 && minC <= 59) {
		result = ` ${parseInt(minC, 10)}分钟前`;
	} else if (diffValue >= 0 && diffValue <= minute) {
		result = '刚刚';
	} else {
		const datetime = new Date();
		datetime.setTime(dateTimeStamp);
		const Nyear = datetime.getFullYear();
		const Nmonth = datetime.getMonth() + 1 < 10 ? `0${datetime.getMonth() + 1}` : datetime.getMonth() + 1;
		const Ndate = datetime.getDate() < 10 ? `0${datetime.getDate()}` : datetime.getDate();
		result = `${Nyear}-${Nmonth}-${Ndate}`;
	}

	return result;
}
export function formateTime(secondTime) {
	const time = secondTime;
	let newTime;
	let hour;
	let minite;
	let seconds;
	if (time >= 3600) {
		hour = parseInt(time / 3600) < 10 ? `0${parseInt(time / 3600)}` : parseInt(time / 3600);
		minite = parseInt(time % 60 / 60) < 10 ? `0${parseInt(time % 60 / 60)}` : parseInt(time % 60 / 60);
		seconds = time % 3600 < 10 ? `0${time % 3600}` : time % 3600;
		if (seconds > 60) {
			minite = parseInt(seconds / 60) < 10 ? `0${parseInt(seconds / 60)}` : parseInt(seconds / 60);
			seconds = seconds % 60 < 10 ? `0${seconds % 60}` : seconds % 60;
		}
		newTime = `${hour}:${minite}:${seconds}`;
	} else if (time >= 60 && time < 3600) {
		minite = parseInt(time / 60) < 10 ? `0${parseInt(time / 60)}` : parseInt(time / 60);
		seconds = time % 60 < 10 ? `0${time % 60}` : time % 60;
		newTime = `00:${minite}:${seconds}`;
	} else if (time < 60) {
		seconds = time < 10 ? `0${time}` : time;
		newTime = `00:00:${seconds}`;
	}
	return newTime;
}
// 将字符串编码为 Uint8Array
export function encodeString(str) {
	var utf8Array = [];
	for (var i = 0; i < str.length; i++) {
		var charCode = str.charCodeAt(i); // 获得字符的ASCII
		if (charCode < 0x80) {
			utf8Array.push(charCode);
		} else if (charCode < 0x800) {
			utf8Array.push(0xc0 | (charCode >> 6));
			utf8Array.push(0x80 | (charCode & 0x3f));
		} else if (charCode < 0x10000) {
			utf8Array.push(0xe0 | (charCode >> 12));
			utf8Array.push(0x80 | ((charCode >> 6) & 0x3f));
			utf8Array.push(0x80 | (charCode & 0x3f));
		} else {
			utf8Array.push(0xf0 | (charCode >> 18));
			utf8Array.push(0x80 | ((charCode >> 12) & 0x3f));
			utf8Array.push(0x80 | ((charCode >> 6) & 0x3f));
			utf8Array.push(0x80 | (charCode & 0x3f));
		}
	}
	return new Uint8Array(utf8Array);
}
// 字符串转为base64
export function base64Encode(inputString) {
	const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-/=";
	let inputBytes = encodeString(inputString);
	let output = "";
	let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
	let i = 0;
	while (i < inputBytes.length) {
		chr1 = inputBytes[i++];
		chr2 = inputBytes[i++];
		chr3 = inputBytes[i++];
		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;
		if (isNaN(chr2)) {
			enc3 = enc4 = 64;
		} else if (isNaN(chr3)) {
			enc4 = 64;
		}
		output +=
			keyStr.charAt(enc1) +
			keyStr.charAt(enc2) +
			keyStr.charAt(enc3) +
			keyStr.charAt(enc4);
	}
	return output;
}